using System;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
public class precolor_range_class
{
	public bool foldout;

	public bool color_ranges_active;

	public bool color_ranges_foldout;

	public bool palette;

	public AnimationCurve curve_red;

	public AnimationCurve curve_green;

	public AnimationCurve curve_blue;

	public animation_curve_math_class animation_curve_math;

	public bool interface_display;

	public int index;

	public bool one_color;

	public Rect menu_rect;

	public string text;

	public Rect rect;

	public int detect_max;

	public List<color_range_class> color_range;

	public value_class color_range_value;

	public precolor_range_class(int length, bool one_color1)
	{
		foldout = true;
		color_ranges_active = true;
		color_ranges_foldout = true;
		curve_red = new AnimationCurve();
		curve_green = new AnimationCurve();
		curve_blue = new AnimationCurve();
		animation_curve_math = new animation_curve_math_class();
		interface_display = true;
		text = "Color Range:";
		detect_max = 8;
		color_range = new List<color_range_class>();
		color_range_value = new value_class();
		for (int i = 0; i < length; i++)
		{
			add_color_range(i, one_color1);
		}
		one_color = one_color1;
		set_color_range_text();
		set_precolor_range_curve();
	}

	public void add_color_range(int color_range_number, bool one_color)
	{
		color_range.Insert(color_range_number, new color_range_class());
		color_range[color_range_number].one_color = one_color;
		color_range[color_range_number].select_output = color_range_number;
		color_range_value.add_value(color_range_number, 50f);
		set_precolor_range_curve();
		set_color_range_text();
	}

	public void erase_color_range(int color_range_number)
	{
		if (color_range.Count > 0)
		{
			color_range.RemoveAt(color_range_number);
			color_range_value.erase_value(color_range_number);
			set_precolor_range_curve();
			set_color_range_text();
		}
	}

	public void clear_color_range()
	{
		color_range.Clear();
		color_range_value.clear_value();
		set_precolor_range_curve();
		set_color_range_text();
	}

	public void invert_color_range(int color_range_number)
	{
		float num = 0.003921569f;
		color_range_value.active[color_range_number] = false;
		add_color_range(color_range_number + 1, one_color: false);
		add_color_range(color_range_number + 1, one_color: false);
		if (color_range[color_range_number].color_start != new Color(0f, 0f, 0f))
		{
			if (color_range_number < 2)
			{
				color_range[color_range_number + 1].color_start = new Color(0f, 0f, 0f);
			}
			else
			{
				color_range[color_range_number + 1].color_start = color_range[color_range_number - 1].color_end + new Color(num, num, num);
			}
			color_range[color_range_number + 1].color_end = color_range[color_range_number].color_start + new Color(0f - num, 0f - num, 0f - num);
		}
		if (color_range[color_range_number].color_end != new Color(1f, 1f, 1f))
		{
			color_range[color_range_number + 2].color_start = color_range[color_range_number].color_end + new Color(num, num, num);
			if (color_range.Count - 1 == color_range_number + 2)
			{
				color_range[color_range_number + 2].color_end = new Color(1f, 1f, 1f);
			}
			else
			{
				color_range[color_range_number + 2].color_end = color_range[color_range_number + 3].color_start + new Color(0f - num, 0f - num, 0f - num);
			}
		}
	}

	public void set_color_range_text()
	{
		text = "Color Range" + index + " (" + color_range.Count + ")";
	}

	public void swap_color(int color_range_number1, int color_range_number2)
	{
		color_range_class color_range_class = null;
		float num = default(float);
		if (color_range_number2 > -1 && color_range_number2 < color_range.Count)
		{
			color_range_class = color_range[color_range_number1];
			color_range[color_range_number1] = color_range[color_range_number2];
			color_range[color_range_number2] = color_range_class;
			if (!(color_range[color_range_number1].color_color_range[0] >= 1.5f))
			{
				color_range[color_range_number1].color_color_range = color_range[color_range_number1].color_color_range + new Color(1f, 1f, 1f, 1f);
			}
			if (!(color_range[color_range_number2].color_color_range[0] >= 1.5f))
			{
				color_range[color_range_number2].color_color_range = color_range[color_range_number2].color_color_range + new Color(1f, 1f, 1f, 1f);
			}
			color_range_value.swap_value(color_range_number1, color_range_number2);
			set_precolor_range_curve();
		}
	}

	public void detect_colors_image(Texture2D texture)
	{
		int num = 0;
		Color color = default(Color);
		int num2 = default(int);
		bool flag = false;
		for (int i = 0; i < texture.height; i++)
		{
			for (int j = 0; j < texture.width; j++)
			{
				color = texture.GetPixel(j, i);
				flag = false;
				for (num2 = 0; num2 < color_range.Count; num2++)
				{
					if (color == color_range[num2].color_start)
					{
						flag = true;
					}
				}
				if (!flag)
				{
					add_color_range(color_range.Count, one_color: true);
					color_range[color_range.Count - 1].color_start = color;
					num++;
					if (num > detect_max - 1)
					{
						return;
					}
				}
			}
		}
	}

	public void set_precolor_range_curve()
	{
		curve_red = new AnimationCurve();
		curve_green = new AnimationCurve();
		curve_blue = new AnimationCurve();
		float num = color_range.Count;
		if (!(num <= 1f))
		{
			float num2 = 1f / (num - 1f);
			for (int i = 0; (float)i < num; i++)
			{
				int num3 = default(int);
				num3 = curve_red.AddKey((float)i * num2, color_range[i].color_start.r);
				num3 = curve_green.AddKey((float)i * num2, color_range[i].color_start.g);
				num3 = curve_blue.AddKey((float)i * num2, color_range[i].color_start.b);
			}
		}
		else if (num == 1f)
		{
			curve_red.AddKey(0f, color_range[0].color_start.r);
			curve_red.AddKey(1f, color_range[0].color_start.r);
			curve_green.AddKey(0f, color_range[0].color_start.g);
			curve_green.AddKey(1f, color_range[0].color_start.g);
			curve_blue.AddKey(0f, color_range[0].color_start.b);
			curve_blue.AddKey(1f, color_range[0].color_start.b);
		}
		curve_red = animation_curve_math.set_curve_linear(curve_red);
		curve_green = animation_curve_math.set_curve_linear(curve_green);
		curve_blue = animation_curve_math.set_curve_linear(curve_blue);
	}
}
